﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using DTO;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http.Extensions;
using OtherLib.TenXun;
using FacadeApi.Extension;
using OtherLib.AliPay;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class CallBackController : ControllerBase
    {
        public PayApplication payApplication { get; set; }
        public ILogger<UserController> _logger { get; set; }

        [HttpPost, NotHamcSign]
        public string AliPayNotify([FromForm]AliPayNotifyInput aliPayNotifyInput)
        {
            try
            {
                var r = payApplication.AliPayNotify(Request.Form.ToDictionary(key=>key.Key,value=>value.Value.ToString()),aliPayNotifyInput);

                return "OK";
            }
            catch (BizException ex)
            {
                _logger.LogException(ex);
                return "OK";
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return "OK";
            }

        }

        [HttpPost, NotHamcSign]
        public string WxNotifyApp()
        {
            //string result = @"<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[NOOK]]></return_msg></xml>";
            string result = @"<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
            try
            {
                //_logger.LogInformation("支付宝支付回调:  " + JsonConvert.SerializeObject(Request.Form));
                var r = payApplication.WxPayNotify(Request.Body,"App");

                
            }
            catch (BizException ex)
            {
                _logger.LogException(ex);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return result;
            }
            return result;
        }
        [HttpPost, NotHamcSign]
        public string WxNotifyMini()
        {
            //string result = @"<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[NOOK]]></return_msg></xml>";
            string result = @"<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
            try
            {
                Console.WriteLine("WxNotifyMini");
                //_logger.LogInformation("支付宝支付回调:  " + JsonConvert.SerializeObject(Request.Form));
                var r = payApplication.WxPayNotify(Request.Body, "Mini");
            }
            catch (BizException ex)
            {
                _logger.LogException(ex);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return result;
            }
            return result;
        }
    }
}
